//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by mac on 16/2/18.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //布局子视图的方法
    override func layoutSubviews() {
        super.layoutSubviews()
        //子视图有哪些视图
//        print(subviews)
        
        //确定tabBarButton 的大小
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个能够递增的索引
        var index: CGFloat = 0
        
        //遍历子视图 获取UITabBarButton
        for subView in subviews{
        
            //保证通过UITabBarButton 字符串 可以获取 对应的类型
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
//                print(subviews)
                //修改frame
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                //把中间留出来
//                if index == 1{
//                
//                    index++
//                }
//                
//                index++
                //简化成三目运算
                index += (index == 1 ? 2 : 1)
            
            }
        }
        
        //设置加号按钮frame
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    //懒加载  加号按钮
    lazy var plusBtn :UIButton = UIButton(backgroundImageName: "tabbar_compose_button", imageName: "tabbar_compose_icon_add")
//    {
//    
//        let btn = UIButton()
//        
//        //设置背景图片
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
//        
//        //设置图片
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal )
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted "), forState: .Highlighted)
//        
//        self.addSubview(btn)
//        
//        return btn
//    }()
//    
    
    
}
